import { Banner } from '@/api/home'
import http from '@/utils/request'
import { defineStore } from 'pinia'
import { CategoryHead, Hot, New } from './home.types'

const home = defineStore({
  // 唯一标识
  id: 'home',
  // 开启持久化插件
  persist: true,
  // 相当于 data
  state: () => {
    return {
      category: [] as CategoryHead[],
      banner: [] as Banner[],
      new: [] as New[],
      hot: [] as Hot[]
    }
  },
  // 相当于 computed
  getters: {},
  // 相当于 methods
  actions: {
    // ✨ 可以直接写异步或者同步逻辑
    //  首页-头部分类-PC
    async findHomeCategoryHead () {
      const { result } = await http<CategoryHead[]>('/home/category/head', 'get')
      this.category = result
    },
    // 获取轮播图
    async findHomeBanner  () {
      const { result } = await http<Banner[]>('/home/banner', 'get')
      this.banner = result
    },
    // 获取新鲜好物
    async findHomeNew (limit = 4) {
      const { result } = await http<New[]>('/home/new', 'get', { limit: limit })
      this.new = result
    },
    // 获取人气推荐
    async findHot () {
      const { result } = await http<Hot[]>('home/hot', 'get')
      this.hot = result
    }
  }
  // 🚧注意：Pinia 没有 mutations
})

// const home = home()
// home.findHot()

export default home
